﻿using Abp.Application.Services;
using Abp.Domain.Repositories;
using AbpDemo.Entity;
using study.Scores.Dto;
using study.Students.Dto;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study.Scores
{
    public class ScoreAppService :studyAppServiceBase
    {

        private readonly IRepository<Score,int> _scoreRepository;
        private readonly IRepository<Course,long> _courseRepository;
        private readonly IRepository<Student> _studentRepository;
        public ScoreAppService(IRepository<Score,int> scoreRepository, IRepository<Course, long> courseRepository, IRepository<Student> studentRepository)
        {
            _scoreRepository = scoreRepository;
            _courseRepository = courseRepository;
            _studentRepository = studentRepository;
        }

        /// <summary>
        /// 获取学生数据
        /// </summary>
        /// <returns></returns>
        public List<OutPutScore> OutPutAllScore(ScoreDto input)
        {
            var scoreList = _scoreRepository.GetAll();
            if (input.StudentId !="-1")
            {
                scoreList = scoreList.Where(s => s.StudentId == Convert.ToInt32(input.StudentId));
            }
            if (input.CourseId != "-1")
            {
                scoreList = scoreList.Where(s => s.CourseId == Convert.ToInt32(input.CourseId));
            }
            List<OutPutScore> output = new List<OutPutScore>();
            List<Score> allList = scoreList.ToList();
            foreach (Score score in allList)
            {
                
                var studentName = _studentRepository.Get(score.StudentId).Name;
                var courseName = _courseRepository.Get(score.CourseId).CourseName;
                output.Add(new OutPutScore(score.Id, studentName, courseName, score.StudentId, score.CourseId, score.Grade.ToString()));
            }
            
            return output;
        }

        /// <summary>
        /// 根据学生级联获取数据
        /// </summary>
        /// <returns></returns>
        public List<Course> FindByStudentId(ScoreDto input)
        {
            var scoreList = _scoreRepository.GetAll().ToList();

            scoreList = scoreList.Where(s => s.StudentId == Convert.ToInt32(input.StudentId)).ToList();

            var courseList = _courseRepository.GetAll();
            List<Course> outputcourseList = new List<Course>();
            foreach (Course course in courseList)
            {
                int count = scoreList.Where(s => s.CourseId == course.Id).Count();
                if (count <= 0)
                {
                    outputcourseList.Add(course);
                }
            }
           
            return outputcourseList;
        }

        /// <summary>
        /// 更新学生
        /// </summary>
        /// <returns></returns>
        public void UpdateScore(ScoreDto input)
        {

            Score score = _scoreRepository.Get(input.Id);
            score.Grade = input.Grade;
            _scoreRepository.Update(score);
        }

        /// <summary>
        /// 删除学生
        /// </summary>
        /// <returns></returns>
        public void DelScore(ScoreDto input)
        {
            Score score = _scoreRepository.Get(Convert.ToInt32(input.Id));
            _scoreRepository.Delete(score);
        }

        /// <summary>
        /// 新增学生
        /// </summary>
        /// <returns></returns>
        public void CreateScore(CreateScoreDto input)
        {
            var score = new Score() { StudentId = input.StudentId, CourseId = input.CourseId, Grade = input.Grade };
            _scoreRepository.Insert(score);
        }
        
    }
}
